package cn.fldong.service.impl;

import cn.fldong.dao.ClassDAO;
import cn.fldong.dao.impl.ClassDAOImpl;
import cn.fldong.model.ClassInfo;
import cn.fldong.service.ClassService;
import java.sql.SQLException;
import java.util.List;

/**
 * ClassService接口的实现类，提供班级相关的业务逻辑实现
 */
public class ClassServiceImpl implements ClassService {
    private ClassDAO classDAO = new ClassDAOImpl();

    @Override
    public ClassInfo getClassById(String classId) throws SQLException {
        return classDAO.getClassById(classId);
    }

    @Override
    public List<ClassInfo> getAllClasses() throws SQLException {
        return classDAO.getAllClasses();
    }

    @Override
    public List<ClassInfo> getClassesByGrade(String grade) throws SQLException {
        return classDAO.getClassesByGrade(grade);
    }

    @Override
    public List<ClassInfo> getClassesByMajor(String major) throws SQLException {
        return classDAO.getClassesByMajor(major);
    }

    @Override
    public boolean addClass(ClassInfo classInfo) throws SQLException {
        // 检查班级名称是否已存在
        if (classNameExists(classInfo.getClassName())) {
            return false;
        }
        return classDAO.addClass(classInfo);
    }

    @Override
    public boolean updateClass(ClassInfo classInfo) throws SQLException {
        // 检查班级名称是否已存在（排除当前班级）
        ClassInfo existingClass = getClassById(classInfo.getClassId());
        if (!existingClass.getClassName().equals(classInfo.getClassName()) && classNameExists(classInfo.getClassName())) {
            return false;
        }
        return classDAO.updateClass(classInfo);
    }

    @Override
    public boolean deleteClass(String classId) throws SQLException {
        // 实际应用中可能需要先检查该班级是否有关联的学生
        return classDAO.deleteClass(classId);
    }

    @Override
    public boolean classNameExists(String className) throws SQLException {
        return classDAO.classNameExists(className);
    }

    @Override
    public boolean hasStudents(String classId) throws SQLException {
        return classDAO.hasStudents(classId);
    }

    @Override
    public List<ClassInfo> searchClassesByClassName(String keyword) throws SQLException {
        return classDAO.searchClassesByClassName(keyword);
    }

    @Override
    public List<ClassInfo> searchClassesByClassId(String keyword) throws SQLException {
        return classDAO.searchClassesByClassId(keyword);
    }

    @Override
    public List<ClassInfo> searchClassesByMajor(String keyword) throws SQLException {
        return classDAO.searchClassesByMajor(keyword);
    }

    @Override
    public List<ClassInfo> searchClassesByGrade(String keyword) throws SQLException {
        return classDAO.searchClassesByGrade(keyword);
    }
}